import request, { toFormdata } from '@/utils/request';
import type { PagedList } from '../typing';

//${view.entityComment}对象
export interface ${view.entityTypeSimpleName} {
<#list view.propertyList as property>
    <#if (property.propertyName == "parentId")>
  parentId:string;
  children?: ${view.entityTypeSimpleName}[];
    <#else>
        <#if property.isSimpleValueType()>
            <#if property.propertyTypeSimpleName == 'boolean'>
  ${property.propertyName}?: boolean; //${property.propertyComment}
  			<#elseif property.propertyTypeSimpleName == 'Boolean'>
  ${property.propertyName}?: string; //${property.propertyComment}
            <#elseif property.isEnum()>
  ${property.propertyName}?: string; //${property.propertyComment}
            <#elseif  property.propertyTypeSimpleName == 'Date'>
  ${property.propertyName}?: string; //${property.propertyComment}
            <#elseif  property.propertyTypeSimpleName == 'int'
            	|| property.propertyTypeSimpleName == 'long'
                || property.propertyTypeSimpleName == 'double'
                || property.propertyTypeSimpleName == 'float'>
  ${property.propertyName}?: number; //${property.propertyComment}
  			<#elseif  property.propertyTypeSimpleName == 'Intengrt'
  				|| property.propertyTypeSimpleName == 'Long'
                || property.propertyTypeSimpleName == 'BigDecimal'
                || property.propertyTypeSimpleName == 'Double'
                || property.propertyTypeSimpleName == 'Float'>
  ${property.propertyName}?: string; //${property.propertyComment}
  			<#elseif  property.isEnum()>
  ${property.propertyName}?: string; //${property.propertyComment}
            <#else>
  ${property.propertyName}?: string; //${property.propertyComment}
            </#if>
        <#else>
  '${property.columnPropertyName}'?: string; //${property.propertyComment}
        </#if>
    </#if>
</#list>
}

//查询${view.entityComment}id
export async function queryList(params?: { [key: string]: any }): Promise<${view.entityTypeSimpleName}[]>{
  return request.get<any, ${view.entityTypeSimpleName}[]>(`/${view.entityTypeSimpleName?replace("([a-z])([A-Z]+)","$1_$2","r")?lower_case}/list`, { params });
}

<#if (view.parentIdProperty?? || view.parentProperty??)>
//查询子级${view.entityComment}列表
export async function queryChildren(parentId: string, params?: { [key: string]: any }): Promise<${view.entityTypeSimpleName}[]>{
  return request.get<any, ${view.entityTypeSimpleName}[]>(`/${view.entityTypeSimpleName?replace("([a-z])([A-Z]+)","$1_$2","r")?lower_case}/descendants/${r"${"}parentId}`, { params });
}

//查询子孙${view.entityComment}列表
export async function queryDescendants(parentId: string, params?: { [key: string]: any }): Promise<${view.entityTypeSimpleName}[]>{
  return request.get<any, ${view.entityTypeSimpleName}[]>(`/${view.entityTypeSimpleName?replace("([a-z])([A-Z]+)","$1_$2","r")?lower_case}/descendants/${r"${"}parentId}`, { params });
}
    
</#if>
//查询${view.entityComment}分页列表
export async function queryPagedList(pageSize: number, pageNumber: number, params?: { [key: string]: any }): Promise<PagedList<${view.entityTypeSimpleName}>> {
  return request.get<any, PagedList<${view.entityTypeSimpleName}>>(`/${view.entityTypeSimpleName?replace("([a-z])([A-Z]+)","$1_$2","r")?lower_case}/pages/${r"${"}pageSize}/${r"${"}pageNumber}`, { params });
}

//根据id查询${view.entityComment}
export async function findById(id: string) {
  return request.get<any, ${view.entityTypeSimpleName}>(`/${view.entityTypeSimpleName?replace("([a-z])([A-Z]+)","$1_$2","r")?lower_case}/${r"${"}id}`);
}

<#if view.codeProperty??>
//根据code查询${view.entityComment}
export async function findByCode(code: string) {
  return request.get<any, ${view.entityTypeSimpleName}>(`/${view.entityTypeSimpleName?replace("([a-z])([A-Z]+)","$1_$2","r")?lower_case}/code/${r"${"}code}`);
}

</#if>
//新增${view.entityComment}
export async function add(data: ${view.entityTypeSimpleName}) {
  return request.post<${view.entityTypeSimpleName}, boolean>(`/${view.entityTypeSimpleName?replace("([a-z])([A-Z]+)","$1_$2","r")?lower_case}`, toFormdata(data));
}

//根据id删除${view.entityComment}
export async function deleteById(id: string) {
  return request.delete<any, boolean>(`/${view.entityTypeSimpleName?replace("([a-z])([A-Z]+)","$1_$2","r")?lower_case}/${r"${"}id}`);
}

//更新${view.entityComment}
export async function update(id: string, data: ${view.entityTypeSimpleName}) {
  return request.put<${view.entityTypeSimpleName}, boolean>(`/${view.entityTypeSimpleName?replace("([a-z])([A-Z]+)","$1_$2","r")?lower_case}/${r"${"}id}`, toFormdata(data));
}

<#if view.validProperty??>
//禁用${view.entityComment}
export async function disableById(id: string) {
  return request.patch<any, boolean>(`/${view.entityTypeSimpleName?replace("([a-z])([A-Z]+)","$1_$2","r")?lower_case}/disable/${r"${"}id}`);
}

//启用${view.entityComment}
export async function enableById(id: string) {
  return request.patch<any, boolean>(`/${view.entityTypeSimpleName?replace("([a-z])([A-Z]+)","$1_$2","r")?lower_case}/enable/${r"${"}id}`);
}

</#if>
//判断${view.entityComment}是否存在
export async function exists(params: { [key: string]: any }, exclude${view.pkProperty.propertyName?cap_first}?: string) {
  if(excludeId && excludeId.length > 0){
    params['exclude${view.pkProperty.propertyName?cap_first}'] = exclude${view.pkProperty.propertyName?cap_first};
  }
  return request.get<any, boolean>(`/${view.entityTypeSimpleName?replace("([a-z])([A-Z]+)","$1_$2","r")?lower_case}/exists`,{ params });
}